<template>
  <div id="box">
    <div class="box2">
      <div id="box2" class="box3 ">

        <el-form ref="xxx" :model="from" :rules="rules">
          <el-form-item prop="mobile">
            <el-input v-model="from.mobile" prefix-icon="el-icon-user-solid" />
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              v-model="from.password"
              prefix-icon="el-icon-user-solid"
              show-password
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="width: 30%" @click="handleLogin">登录</el-button>

            <el-button type="primary" style="width: 30%" @click="zc">注册</el-button>
          </el-form-item>

        </el-form>

      </div>
    </div>
  </div>
</template>

<script>
import { AestheticFluidBg } from '@/color4bg.js/build/jsm/AestheticFluidBg.module.js'
import { login } from '@/api/user'
// import { ChaosWavesBg } from '@/color4bg.js/build/jsm/ChaosWavesBg.module.js'
import { register } from '@/api/user'
export default {
  name: 'Login',
  data() {
    return {
      from: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          { required: true, message: '不能为空', trigger: 'change' },
          { min: 11, max: 11, message: '11位谢谢', trigger: 'change' }
        ],
        password: [
          { required: true, message: '不能为空', trigger: 'change' },
          { message: '输入6-11位账号', min: 6, max: 11, trigger: 'change' }
        ]
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      new AestheticFluidBg({
        dom: 'box',
        colors: [
          '#FDFDFD',
          '#DDDDDD',
          '#BBBBBB',
          '#555555',
          '#343434',
          '#010101'
        ],
        seed: 1000,
        loop: true
      })

      // new ChaosWavesBg({
      //   dom: 'box2',
      //   colors: ['#FDFDFD', '#DDDDDD', '#BBBBBB', '#555555', '#343434', '#010101'],
      //   loop: true
      // })
    })
  },
  methods: {
    async  zc() {
      try {
        await this.$refs.xxx.validate()
        await register({
          mobile: this.from.mobile,
          password: this.from.password
        })
        this.$message.success('注册成功')
      } catch (error) {
        this.$message.error(error.message || '注册失败')
      }
    },
    // 登录
    async handleLogin() {
      try {
        await this.$refs.xxx.validate()
        const res = await login({
          username: this.from.mobile, // mock使用username字段
          password: this.from.password
        })
        console.log(res)
        this.$store.commit('user/SET_TOKEN', res.data.token)
        this.$store.commit('user/SET_ROLES', res.data.roles)
        this.$store.commit('user/SET_NAME', res.data.name)
        this.$store.commit('user/SET_AVATAR', res.data.avatar)
        this.$store.commit('user/SET_INTRODUCTION', res.data.introduction)
        this.$message.success('登录成功')
        this.$router.push('/')
        // 存储token等后续操作
      } catch (error) {
        this.$message.error(error.message || '登录失败')
        console.error('登录错误详情:', error)
      }
    }
  }
}
</script>

<style lang="scss" scoped >
#box {
  width: 100vw;
  height: 100vh;
  position: relative;

  .box2 {
    background-color: red;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;

    .box3{
      z-index: 3;
  //  width: 360px;
  //  height: 400px;
  // //  background-color: red;
   display: flex;
    justify-content: center;
    align-items: center;
      width: 400px;
      background-color: rgb(255, 255, 255,0.2);

  padding: 50px 20px;
.el-form {

     z-index: 999;
      width: 300px;

      ::v-deep .el-input__inner {
        height: 43px;
        margin-bottom: 5px;
      }

      ::v-deep .el-form-item__content {
        margin-bottom: 10px;
      }

      ::v-deep .el-button--primary {
        padding: 13px;
      }
    }
    }

  }
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
::v-deep .el-form-item__content{
  display: flex;

  justify-content: space-between;

  margin-top: 30px;

}
</style>
